import {SimpleModel} from "../../framework/model/SimpleModel.ts";
import type {Material} from "three";
import * as THREE from 'three'


// 地板
export class Floor extends SimpleModel {

    plant_width: number
    plant_height: number
    thickness: number
    material: Material
    materialRed: Material
    mesh: THREE.Mesh
    color: number
    baseMesh!: THREE.Mesh

    constructor() {
        super("floor")
        this.material = new THREE.MeshPhongMaterial({color: 0x2d394e, depthWrite: false})
        this.materialRed = new THREE.MeshPhongMaterial({
            color: 0x2d394e
        })
    }

    public init(plant_width: number, plant_height: number, thickness: number, color: number, walls: boolean) {

        this.plant_height = plant_height
        this.plant_width = plant_width
        this.thickness = thickness
        this.color = color

        this.baseMesh = new THREE.Mesh(
            new THREE.BoxGeometry(1, 1, 1),
            this.material
        )

        this.mesh = this.baseMesh.clone()

        this.mesh.scale.set(this.plant_width, this.plant_height, this.thickness)

        this.scene?.add(this.mesh)

        this.mesh.position.y = this.thickness / 2
        this.mesh.rotation.x = Math.PI / 2; // 绕X轴旋转-90度

        if (walls) this.createWalls()
    }

    // 创建墙
    private createWalls() {
        this.createLeft()
        // this.createRight()
        this.createFont()
        this.createBack()
    }

    // 左墙
    private createLeft(): void {

        let wall_length = this.plant_height//长
        let wall_height = 2// 高
        let wall_width = this.thickness

        let mesh = this.baseMesh.clone()
        mesh.material = this.materialRed
        mesh.scale.set(wall_width, wall_length, wall_height)
        mesh.position.setX(-this.plant_width / 2)
        mesh.position.setZ(wall_height / 2)

        mesh.rotation.x = Math.PI / 2; // 绕X轴旋转-90度

        this.scene?.add(mesh)
    }

    // 右墙
    private createRight(): void {

        let wall_length = this.plant_height//长
        let wall_height = 2// 高
        let wall_width = this.thickness

        let mesh = this.baseMesh.clone()
        mesh.material = this.materialRed
        mesh.scale.set(wall_width, wall_length, wall_height)
        mesh.position.setX(this.plant_width / 2)
        mesh.position.setZ(wall_height / 2)
        mesh.rotation.x = Math.PI / 2; // 绕X轴旋转-90度

        this.scene?.add(mesh)
    }

    // 前墙
    private createFont(): void {

        let wall_length = this.plant_width//长
        let wall_height = 2// 高
        let wall_width = this.thickness

        let mesh = this.baseMesh.clone()
        mesh.material = this.materialRed
        mesh.scale.set(wall_width, wall_length, wall_height)
        mesh.position.setX(0)
        mesh.position.setZ(this.plant_height / 2)
        mesh.position.setY(0)

        mesh.rotation.z = -Math.PI / 2; // 绕Z轴旋转-90度
        mesh.rotation.x = Math.PI / 2; // 绕X轴旋转-90度

        this.scene?.add(mesh)
    }

    // 后墙
    private createBack(): void {

        let wall_length = this.plant_width//长
        let wall_height = 2// 高
        let wall_width = this.thickness

        let mesh = this.baseMesh.clone()
        mesh.material = this.materialRed
        mesh.scale.set(wall_width, wall_length, wall_height)
        mesh.position.setX(0)
        mesh.position.setZ(-this.plant_height / 2)
        mesh.position.setY(0)
        mesh.rotation.z = -Math.PI / 2; // 绕Z轴旋转-90度
        mesh.rotation.x = Math.PI / 2; // 绕X轴旋转-90度

        this.scene?.add(mesh)
    }
}


